<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>One to One Foreign Key Association</title>
</head>
<body>
  <h3>One to One - Foreign Key Association</h3>
  One of the most common ways to map one to one relationship between two entities is using a foreign key relationship.
  Let's take an example. We'll map Employee to Desk relationship. An employee will be allocated a desk and a desk can just have
  one employee sitting at. It's true one to one association. Let's consider unidirectional association first. Employee knows about
  his allocated desk. Therefore, Employee class will maintain a reference to Desk. <em>@OneToOne</em> annotation on placed on Desk reference in Employee class defines this one to one association. <em>@JoinColumn</em> annotation placed on the same reference specifies the foreign key column in employee table that points to associated Desk record in Desk table.
  <pre class="brush: java">
	@OneToOne @JoinColumn(name = "desk_id", unique = true)
	public Desk getDesk() {
		return desk;
	}
  </pre>
  The unique constraint placed on the <em>desk_id</em> makes it a one to one association since no two employees can have same
  desk  assigned to them. If we remove this constraint, it would be a one-to-many association.
  <h4>Bi-directional Association</h4>
  What if you have requirement to find out an employee sitting at a particular desk? The Desk class will need to maintain a reference
  back to assigned employee to support this usage scenario. This is achieved by having an employee reference in the Desk class and placing <em>@OneToOne</em> annotation on it as shown below -
  <pre class="brush: java">
	@OneToOne(mappedBy = "desk")
	public Employee getEmployee() {
		return employee;
	}    
  </pre>
  The <em>mappedBy</em> attribute is a mandatory attribute and it identifies the owning side in the relationship and that is Employee
  in this case. Since, Desk is the inverse side in this relationship, it doesn't have to provide <em>JoinColumn</em> information. The <em>mappedBy</em> element refers to the desk attribute in the Employee class. The value of <em>mappedBy</em> is the name of
the attribute in the owning entity that points back to the inverse entity. The database schema remains unchanged.
<p>Here are a few points to remember about bidirectional one-to-one association -</p>
<ul>
  <li>The <em>@JoinColumn</em> annotation is placed on owner of the entity. It's the entity whose table contains the foreign key
  column to other entity table. In our example, it's Employee class.</li>
  <li>The <em>mappedBy</em> element is specified in the <em>@OneToOne</em> annotation in the inverse entity, that is, the entity that doesn't have a foreign key column.</li>
  <li>It's illegal to provide <em>mappedBy</em> element on both sides and also not to provide it on any side.</li>
</ul>
You can find bidirectional one-to-one example in the examples code.
</body>
</html>
